package kumiyunwii.kumi.controller;

import kumiyunwii.kumi.util.ElasticSearchUtil;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.Requests;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.logging.ESLoggerFactory;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.databind.*;

import java.io.IOException;
import java.net.InetAddress;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("Easticsearch")
public class EasticsearchController {

    private final static String article = "article";
    private final static String content = "content";
    private TransportClient client;

    @RequestMapping(value = "/addIndex", method = RequestMethod.GET)
    public String addIndex() throws IOException {
        Settings settings = Settings.builder()
                .put("cluster.name", "docker-cluster")  //连接的集群名
//                .put("client.transport.ignore_cluster_name", true)  //如果集群名不对，也能连接
                .build();
        //创建client
        client = new PreBuiltTransportClient(settings);
        String json = "{" +
                "\"id\":\"kimchy\"," +
                "\"postDate\":\"2013-01-30\"," +
                "\"message\":\"trying out Elasticsearch\"" +
                "}";
        /**
         * 创建索引并添加映射
         * @throws IOException
         */
        CreateIndexRequestBuilder cib = client.admin().indices().prepareCreate(article);
        XContentBuilder mapping = XContentFactory.jsonBuilder()
                .startObject()
                .startObject("properties") //设置之定义字段
                .startObject("author")
                .field("type", "string") //设置数据类型
                .endObject()
                .startObject("title")
                .field("type", "string")
                .endObject()
                .startObject("content")
                .field("type", "string")
                .endObject()
                .startObject("price")
                .field("type", "string")
                .endObject()
                .startObject("view")
                .field("type", "string")
                .endObject()
                .startObject("tag")
                .field("type", "string")
                .endObject()
                .startObject("date")
                .field("type", "date")  //设置Date类型
                .field("format", "yyyy-MM-dd HH:mm:ss") //设置Date的格式
                .endObject()
                .endObject()
                .endObject();
        cib.addMapping(content, mapping);

        CreateIndexResponse res = cib.execute().actionGet();

        System.out.println("----------添加映射成功----------");
        return "----------添加映射成功----------";
    }

    @RequestMapping(value = "/addIndexDocument", method = RequestMethod.GET)
    public String addIndexDocument() throws Exception {
        Date time = new Date();

        IndexResponse response = client.prepareIndex(article, content)
                .setSource(XContentFactory.jsonBuilder().startObject()
                        .field("id", "447")
                        .field("author", "fendo")
                        .field("title", "192.138.1.2")
                        .field("content", "这是JAVA有关的书籍")
                        .field("price", "20")
                        .field("view", "100")
                        .field("tag", "a,b,c,d,e,f")
                        .field("date", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time))
                        .endObject())
                .get();
        return "添加索引成功,版本号：" + response.getVersion();
    }

//    @RequestMapping(value = "/addIndexDocument", method = RequestMethod.GET)
//    public String addIndexDocument() throws Exception {
//
//    }
}
